﻿using System.Text;
using System.Numerics;
using System;

namespace Core
{
    public class Converter
    {
        public static byte[] ToBytes(string s)
        {
            int length = s.Length;
            byte[] bytes = new byte[length];

            for (int i = 0; i < length; ++i)
            {
                bytes[i] = (byte)s[i];
            }

            return bytes;
        }

        public static string BytesToString(byte[] bytes)
        {
            int length = bytes.Length;
            StringBuilder sb = new StringBuilder(length);
            char[] chars;

            // To chars
            chars = ToChars(bytes);

            // copy to string builder
            sb.Append(chars);

            return sb.ToString();
        }

        private static char[] ToChars(byte[] bytes)
        {
            int length = bytes.Length;
            char[] chars = new char[length];

            for (int i = 0; i < length; ++i)
            {
                chars[i] = (char)bytes[i];
            }

            return chars;
        }

        public static string ToString(BigInteger[] bigInts)
        {
            StringBuilder sb = new StringBuilder();

            foreach (BigInteger b in bigInts)
            {
                sb.Append(b + " ");
            }
            sb.Remove(sb.Length - 1, 1);

            return sb.ToString();
        }

        public static BigInteger[] ToBigInts(string s)
        {
            s = s.Trim();
            string[] ss =  s.Split(' ');
            int length = ss.Length;
            BigInteger[] bis = new BigInteger[length];

            for (int i = 0; i < length; ++i)
            {
                bis[i] = BigInteger.Parse(ss[i]);
            }

            return bis;
        }

        public static string toHex(BigInteger[] bigInts)
        {
            int length = bigInts.Length;
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < length; ++i)
            {
                sb.Append(bigInts[i].ToString("X"));
            }

            return sb.ToString();
        }
    }
}